module AboutYou
  module SDK
    module Model
      ###
      # This class is a represenatation of an Image model
      ###
      class Image
        # min width of an image
        MIN_WIDTH  = 50
        # min height of an image
        MIN_HEIGHT = 50
        # max width of an image
        MAX_WIDTH  = 1400
        # max height of an image
        MAX_HEIGHT = 2000

        class << self
          # base image url
          attr_accessor :base_url
        end
        # Array of additional items
        attr_accessor :additional_items
        # Integer angle of the image
        attr_accessor :angle
        # String background color of image
        attr_accessor :background
        # String color of image
        attr_accessor :color
        # ext
        attr_accessor :ext
        # String gender of the image
        attr_accessor :gender
        # String hash of the image
        attr_accessor :hash
        # instance of AboutYou::SDK::Model::ImageSize
        attr_accessor :image_size
        # Integer file size
        attr_accessor :file_size
        # String focus of the image
        attr_accessor :focus
        # Integer mime type of the image
        attr_accessor :mime_type
        # Hash model data
        attr_accessor :model_data
        # Integer next detail level
        attr_accessor :next_detail_level
        # String preparation
        attr_accessor :preparation
        # Array of String tags
        attr_accessor :tags
        # Integer type
        attr_accessor :type
        # String View
        attr_accessor :view

        ###
        # This method is used for creating an instance of this class by a json_object.
        #
        # * *Args*    :
        #   - +json_object+ -> the json_object received from the api
        #
        # * *Returns* :
        #   - Instance of AboutYou::SDK::Model::Image
        ###
        def self.create_from_json(json_object)
          image = new

          image.additional_items  = json_object['additional_items'] ? json_object['additional_items'] : nil
          image.angle             = json_object['angle'] ? json_object['angle'] : nil
          image.background        = json_object['background'] ? json_object['background'] : nil
          image.color             = json_object['color'] ? json_object['color'] : nil
          image.ext               = json_object['ext'] ? json_object['ext'] : nil
          image.file_size         = json_object['size'] ? json_object['size'] : 0
          image.focus             = json_object['focus'] ? json_object['focus'] : nil
          image.gender            = json_object['gender'] ? json_object['gender'] : nil
          image.hash              = json_object['hash'] ? json_object['hash'] : nil
          image.mime_type         = json_object['mime'] ? json_object['mime'] : nil
          image.model_data        = json_object['model_data'] ? json_object['model_data'] : nil
          image.next_detail_level = json_object['next_detail_level'] ? json_object['next_detail_level'] : nil
          image.preparation       = json_object['preparation'] ? json_object['preparation'] : nil
          image.tags              = json_object['tags'] ? json_object['tags'] : nil
          image.type              = json_object['type'] ? json_object['type'] : nil
          image.view              = json_object['view'] ? json_object['view'] : nil

          image.image_size = ImageSize.new(
            Integer(json_object['image']['width']),
            Integer(json_object['image']['height'])
          )

          image
        end

        ###
        # This method is used for getting the image url for image
        #
        # * *Args*    :
        #   - +width+ -> Width of the image
        #   - +height+ -> Height of the image
        #
        # * *Returns* :
        #   - String url for the image
        ###
        def url(width = 200, height = 200)
          width = [[width, MAX_WIDTH].min, MIN_WIDTH].max
          height = [[height, MAX_HEIGHT].min, MIN_HEIGHT].max

          self.class.base_url + '/' + hash + '?width=' + String(width) +
            '&height=' + String(height)
        end
      end
    end
  end
end
